package com.iqiyi.pps.epg.core.utils;

import com.iqiyi.pps.epg.core.model.program.ChannelStreamData;
import com.iqiyi.pps.epg.core.model.program.LinkData;
import com.iqiyi.pps.epg.core.model.program.ProgramCheckListData;
import com.iqiyi.pps.epg.core.model.program.ProgramData;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by karkarrotxu on 2015/8/17.
 */
public class MailContentUtil {
    public static final String HTML_PRE = "<html><head><meta http-equiv=\"content-type\" content=\"text/html;charset=utf8\"></head>";
    public static final String BODY_PRE = "<body>";
    public static final String BODY_END = "</body>";
    public static final String STYLE_PRE = "<style>";
    public static final String STYLE_END = "</style>";
    public static final String HTML_END = "</html>";
    public static final String TABLE_PRE = "<table style=\"BORDER-COLLAPSE: collapse\" height=\"198\">";
    public static final String TABLE_END = "</table>";
    public static final String TR_PRE = "<tr>";
    public static final String TR_END = "</tr>";
    public static final String TD_CLASS = " .tdClass {FONT-SIZE: 10pt; TEXT-DECORATION: none; BORDER-TOP: #000000 0.5pt solid; HEIGHT: 16.5pt; BORDER-RIGHT: #000000 0.5pt solid; WIDTH: 90.75pt; VERTICAL-ALIGN: middle; BORDER-BOTTOM: #000000 0.5pt solid; FONT-WEIGHT: 400; COLOR: #000000; FONT-STYLE: normal; TEXT-ALIGN: center; BORDER-LEFT: #000000 0.5pt solid; mso-protection: locked visible} " +
            " .tdClassLeft {FONT-SIZE: 10pt; TEXT-DECORATION: none; BORDER-TOP: #000000 0.5pt solid; HEIGHT: 16.5pt; BORDER-RIGHT: #000000 0.5pt solid; WIDTH: 560px; VERTICAL-ALIGN: middle; BORDER-BOTTOM: #000000 0.5pt solid; FONT-WEIGHT: 400; COLOR: #000000; FONT-STYLE: normal; TEXT-ALIGN: left; BORDER-LEFT: #000000 0.5pt solid; mso-protection: locked visible ;  word-break: break-all;  word-wrap: break-word;}" +
            " .tdClassData {FONT-SIZE: 10pt; TEXT-DECORATION: none; BORDER-TOP: #000000 0.5pt solid; HEIGHT: 16.5pt; BORDER-RIGHT: #000000 0.5pt solid; WIDTH: 90.75pt; VERTICAL-ALIGN: middle; BORDER-BOTTOM: #000000 0.5pt solid; FONT-WEIGHT: 400; COLOR: #000000; FONT-STYLE: normal; TEXT-ALIGN: center; BORDER-LEFT: #000000 0.5pt solid; mso-protection: locked visible; min-width:100px; width:100px }" +
            " .tdClassTitle {background:#00cc00 ; FONT-SIZE: 10pt; TEXT-DECORATION: none; BORDER-TOP: #000000 0.5pt solid; HEIGHT: 16.5pt; BORDER-RIGHT: #000000 0.5pt solid; WIDTH: 90.75pt; VERTICAL-ALIGN: middle; BORDER-BOTTOM: #000000 0.5pt solid; FONT-WEIGHT:800; COLOR: #000000; FONT-STYLE: normal; TEXT-ALIGN: center; BORDER-LEFT: #000000 0.5pt solid; mso-protection: locked visible} ";

    public static final String TD_PRE = "<td class=\"tdClass\"  >";
    public static final String TD_PRE_LEFT = "<td class=\"tdClassLeft\"  >";
    public static final String TD_END = "</td>";

    public static final String SPAN_RED = "<span style=\"color:red;\">";
    public static final String SPAN_GREEN = "<span style=\"color:green;\">";
    public static final String SPAN_END = "</span>";
    public static final String BR = "<br>";

    public static final String LB_ADD_PROGRAM_ERROR_TITLE = "轮播台【%s】频道节目添加失败提醒 ";
    public static final String LB_RULE_ERROR_TITLE = "轮播台【%s】频道 规则执行失败提醒 ";
    public static final String ILLEGAL_REMIND_TITLE = "轮播台【%s】频道 自动规则 节目添加失败提醒";
    public static final String LB_EPISODE_NUM_TITLE = "轮播台【%s】频道 自动规则 节目数量预警";
    public static final String CHANGE_TAG_REMIND_TITLE = "临时直播【%s】 分类自动设置提醒";
    public static final String LS_START_REMIND_TITLE = "临时直播【%s】频道";
    public static final String LS_DEL_REQUEST_FAIL_TITLE = "临时直播【%s】频道 删除请求失败";
    public static final String LS_DEL_SUC_TITLE = " 临时直播【%s】频道删除频道成功";
    public static final String LS_DEL_FAIL_REMIND_TITLE = "临时直播【%s】频道到期删除失败提醒";
    public static final String LS_AUTO_DEL_REMIND_TITLE = "临时直播【%s】频道到期提醒";
    public static final String LB_ALBUM_UPDATE_REMIND_TITLE = "轮播台【%s】频道，自动规则预约计划没有节目提醒";
    public static final String CAROUSEL_ERROR_COUNT_REMIND_TITLE = "轮播台【%s】频道,自动规则执行失败";
    public static final String AUTOMATION_RULE_ILLEGAL_TITLE = "轮播台【%s】频道,自动表单规则非法";
    public static final String EPG_SEND_SUFFIX = "<br><br>EPG直播中心—系统邮件。";
    public static final String EPG_CHANNEL_PREFIX = "频道名称：【{channelName}】频道，奇谱ID:{channelQipuId}<br>";
    public static final String EPG_PROGRAM_PREFIX =  "节目名称：【{programName}】节目，奇谱ID:{programQipuId}<br>";
    public static final String EPG_CAROUSEL_PREFIX = "自动规则名称：【{showName}】 ,引用专辑奇谱ID：{carouselQipuId}<br>";
    public static final String EPG_PROGRAM_QIPUIDS = "节目奇谱ID：{programQipuIds}<br> 节目内容不符合轮播台添加规则，已被过滤，请登录后台手动更新节目单！";
    public static final String DAOJU_NEED_REMIND_TITLE = "【%s】节目需要道具运营";
    public static final String DAOJU_MODIFY_REMIND_TITLE = "【%s】节目信息被修改";
    public static final String DAOJU_DISABLED_REMIND_TITLE = "【%s】节目道具被停用";
    public static final String DAOJU_ENABLED_REMIND_TITLE = "【%s】节目道具被重启";
    public static final String STREAM_STOP_REMIND_TITLE = "临时直播频道【%s】流中断";
    public static final String STREAM_START_REMIND_TITLE = "临时直播频道【%s】流延迟";
    public static final String LS_DEL_STREAM_FAIL_REMIND_TITLE = "临时直播【%s】频道 流回收失败";
    public static final String PROGRAM_CREATE_LIVE_NOTIFY_TITLE = "【新建临时直播节目】--%s";
    public static final String TEMPLATE_LB_ADD_PROGRAM_ERROR = EPG_CHANNEL_PREFIX + EPG_PROGRAM_QIPUIDS + EPG_SEND_SUFFIX;
    public static final String TEMPLATE_LB_RULE_ERROR = EPG_CHANNEL_PREFIX + EPG_CAROUSEL_PREFIX + EPG_PROGRAM_QIPUIDS + EPG_SEND_SUFFIX;
    public static final String TEMPLATE_ILLEGAL_REMIND_ERROR = EPG_CHANNEL_PREFIX + EPG_CAROUSEL_PREFIX + "执行创建设定开始时间为 {createStartDate} 的节目<br>" + EPG_PROGRAM_QIPUIDS + EPG_SEND_SUFFIX;
    public static final String TEMPLATE_LB_EPISODE_NUM_ERROR = EPG_CHANNEL_PREFIX + EPG_CAROUSEL_PREFIX + "从lego接口获得数据时，总数为{totalCount}，实际获得{childrenNum}。" + EPG_SEND_SUFFIX;
    // public static final String TEMPLATE_CHANGE_TAG_REMIND = EPG_CHANNEL_PREFIX + "节目名称：【{programName}】节目，奇谱ID:{programQipuId}<br>节目时间{startTime}~{endTime}<br>"+"直播已结束，自动设置分类 正在直播_临时直播" + EPG_SEND_SUFFIX;
    public static final String TEMPLATE_CHANGE_TAG_REMIND = EPG_PROGRAM_PREFIX + "直播已结束，自动设置分类 正在直播_临时直播" + EPG_SEND_SUFFIX;
    public static final String TEMPLATE_LS_START_REMIND = EPG_CHANNEL_PREFIX + "节目名称：【{programName}】节目，奇谱ID:{programQipuId}<br>直播内容即将于{programStartTime}开播，请提前安排直转点相关事项。" + EPG_SEND_SUFFIX;
    public static final String TEMPLATE_LS_DEL_REQUEST_FAIL = EPG_CHANNEL_PREFIX + "删除频道请求失败<br>" + EPG_SEND_SUFFIX;
    public static final String TEMPLATE_LS_DEL_SUC = EPG_CHANNEL_PREFIX + "删除成功<br>" + EPG_SEND_SUFFIX;
    public static final String TEMPLATE_LS_DEL_FAIL_REMIND = EPG_CHANNEL_PREFIX + "删除失败,错误原因：{msg}<br>" + EPG_SEND_SUFFIX;
    public static final String TEMPLATE_LS_AUTO_DEL_REMIND = EPG_CHANNEL_PREFIX + "该频道已经长时间未有直播，即将在{day}天后自动删除，请及时更新节目单。<br>" + EPG_SEND_SUFFIX;
    public static final String TEMPLATE_LB_ALBUM_UPDATE_REMIND = EPG_CHANNEL_PREFIX + "自动专辑：奇谱ID:{carouselQipuId}<br>自动规则时间：{startDay}~{endDay}<br>节目名称：【{programName}】节目，奇谱ID:{programQipuId}<br>节目播放时间：{programStartTime}~{programEndTime}<br>自动规则预约计划失败，尚未收到视频源文件。请及时更新节目单，或通知乐高点播编辑负责人发布视频。" + EPG_SEND_SUFFIX;
    public static final String TEMPLATE_CAROUSEL_ERROR_COUNT_REMIND = EPG_CHANNEL_PREFIX + EPG_CAROUSEL_PREFIX + "执行失败<br>" + EPG_SEND_SUFFIX;
    public static final String TEMPLATE_AUTOMATION_RULE_ILLEGAL_TITLE = EPG_CHANNEL_PREFIX + EPG_CAROUSEL_PREFIX + "规则非法<br>" + EPG_SEND_SUFFIX;
    public static final String TEMPLATE_DAOJU_REMIND = "节目名称：【{programName}】<br>节目奇谱ID： {programQipuId}<br>节目开始播放时间： {programStartTime}<br>节目结束播放时间： {programStopTime}" + EPG_SEND_SUFFIX;
    public static final String TEMPLATE_STREAM_STOP_REMIND = EPG_CHANNEL_PREFIX + "节目名称：【{programName}】<br>节目奇谱ID： {programQipuId}<br> 节目开始播放时间： {programStartTime}<br>节目结束播放时间： {programStopTime} <br> 流名称:{streamName} <br>事件时间:{eventTime}<br>本场直播流有中断。" + EPG_SEND_SUFFIX;
    public static final String TEMPLATE_STREAM_START_REMIND = EPG_CHANNEL_PREFIX + "节目名称：【{programName}】<br>节目奇谱ID： {programQipuId}<br> 节目开始播放时间： {programStartTime}<br>节目结束播放时间： {programStopTime} <br> 流名称:{streamName} <br>事件时间:{eventTime}<br>流延迟,需要校对节目开始时间 " + EPG_SEND_SUFFIX;
    public static final String TEMPLATE_LS_DEL_STREAM_FAIL_REMIND = EPG_CHANNEL_PREFIX + "流回收失败,错误原因：{msg}<br>" + EPG_SEND_SUFFIX;

    public static final String TEMPLATE_PROGRAM_STARTTIME_DELAY_TITLE = "【警告】直播节目没有推流，开始时间自动延后5分钟";
    public static final String TEMPLATE_PROGRAM_STARTTIME_OFFLINE_TITLE = "【警告】直播节目超过20分钟没有推流，直播节目被下线";
    public static final String TEMPLATE_PROGRAM_STARTTIME_DELAY_TITLE_RED = "直播节目到了开播时间没有推流，开播时间自动延后5分钟，请尽快推流！<br>";
    public static final String TEMPLATE_PROGRAM_STARTTIME_DELAY_CONTENT = "节目标题：{programName}<br>节目奇谱ID：{programQipuIds}<br>节目原始开始时间（推迟前的时间）：{programStartTime}<br>";
    public static final String TEMPLATE_PROGRAM_EDITSTARTTIME_DELAY_RED = "节目修改后的开始时间：{delayStartPlayTime}<br>";
    public static final String TEMPLATE_PROGRAM_STOPTIME = "节目结束时间：{programStopTime}<br>";
    public static final String TEMPLATE_PROGRAM_REMIND_RED = "备注：直播节目到开播前3分钟如果仍然没有推流，节目开始时间会自动推迟5分钟，最多推迟20分钟，超过20分钟就会自动下线直播节目。";
    public static final String TEMPLATE_PROGRAM_OFFLINE_TITLE_RED = "直播节目到超过开播时间20分钟没有推流，直播节目被自动下线。<br>";
    public static final String WARN_ASPECT_RATIO = "4:3";

    public static final String TEMPLATE_PROGRAM_CREATE_LIVE_NOTIFY = "节目名称：{programName}<br>频道奇谱ID：{channelQipuId}<br>节目奇谱ID：{programQipuId}<br>节目开始时间： {programStartTime}<br>节目结束时间： {programStopTime}<br>伪静态地址：{programPageUrl}<br>节目创建人：{opUser}<br>节目创建时间：{addTime}<br>节目等级：{episodeGrade}" + EPG_SEND_SUFFIX;


    public static int CLIENT_INUSE_NUMBER = -1;
    private static Logger logger = LoggerFactory.getLogger(MailContentUtil.class);
    private static StringReplace replaceUtil = StringReplace.createUtil("{", "}");

    public enum ContentType {
        CHECK_LIST(1),
        LB_ADD_PROGRAM_ERROR(2),
        LB_RULE_ERROR(3),
        ILLEGAL_REMIND_ERROR(4),
        LB_EPISODE_NUM_ERROR(5),
        CHANGE_TAG_REMIND(6),
        LS_START_REMIND(7),
        LS_DEL_REQUEST_FAIL(8),
        LS_DEL_SUC(9),
        LS_DEL_FAIL_REMIND(10),
        LS_AUTO_DEL_REMIND(11),
        LB_ALBUM_UPDATE_REMIND(12),
        CAROUSEL_ERROR_COUNT_REMIND(13),
        AUTOMATION_RULE_ILLEGAL(14),
        DAOJU_REMIND(15),
        STREAM_START_REMIND(16),
        STREAM_STOP_REMIND(17),
        LS_DEL_STREAM_FAIL_REMIND(18),
        PROGRAM_STARTTIME_DELAY_REMIND(19),
        PROGRAM_STARTTIME_OFFLINE_REMIND(20),
        PROGRAM_CREATE_LIVE_NOTIFY(21);

        private int typeIndex;

        private ContentType(int index) {
            typeIndex = index;
        }

        public int getTypeIndex() {
            return typeIndex;
        }

        public static ContentType getTypeByIndex(int typeIndex) {
            for (ContentType contentType : values()) {
                if (contentType.getTypeIndex() == typeIndex) {
                    return contentType;
                }
            }
            return null;
        }
    }

    public static String getTitleTemplate(ContentType contentType) {
        switch (contentType) {
            case LB_ADD_PROGRAM_ERROR:
                return LB_ADD_PROGRAM_ERROR_TITLE;
            case LB_RULE_ERROR:
                return LB_RULE_ERROR_TITLE;
            case ILLEGAL_REMIND_ERROR:
                return ILLEGAL_REMIND_TITLE;
            case LB_EPISODE_NUM_ERROR:
                return LB_EPISODE_NUM_TITLE;
            case CHANGE_TAG_REMIND:
                return CHANGE_TAG_REMIND_TITLE;
            case LS_START_REMIND:
                return LS_START_REMIND_TITLE;
            case LS_DEL_REQUEST_FAIL:
                return LS_DEL_REQUEST_FAIL_TITLE;
            case LS_DEL_SUC:
                return LS_DEL_SUC_TITLE;
            case LS_DEL_FAIL_REMIND:
                return LS_DEL_FAIL_REMIND_TITLE;
            case LS_AUTO_DEL_REMIND:
                return LS_AUTO_DEL_REMIND_TITLE;
            case LB_ALBUM_UPDATE_REMIND:
                return LB_ALBUM_UPDATE_REMIND_TITLE;
            case CAROUSEL_ERROR_COUNT_REMIND:
                return CAROUSEL_ERROR_COUNT_REMIND_TITLE;
            case AUTOMATION_RULE_ILLEGAL:
                return AUTOMATION_RULE_ILLEGAL_TITLE;
            case STREAM_START_REMIND:
                return STREAM_START_REMIND_TITLE;
            case STREAM_STOP_REMIND:
                return STREAM_STOP_REMIND_TITLE;
            case LS_DEL_STREAM_FAIL_REMIND:
                return LS_DEL_STREAM_FAIL_REMIND_TITLE;
            case PROGRAM_STARTTIME_DELAY_REMIND:
                return TEMPLATE_PROGRAM_STARTTIME_DELAY_TITLE;
            case PROGRAM_STARTTIME_OFFLINE_REMIND:
                return TEMPLATE_PROGRAM_STARTTIME_OFFLINE_TITLE;
            case PROGRAM_CREATE_LIVE_NOTIFY:
                return PROGRAM_CREATE_LIVE_NOTIFY_TITLE;
            default:
                logger.info("not match type:" + contentType);
                return null;
        }
    }

    public static String getContent(ContentType type, Object object) {
        switch (type) {
            case CHECK_LIST:
                return checkList((Map<String, Object>) object);
            case LB_ADD_PROGRAM_ERROR:
                return lbAddProgram((Map<String, String>) object);
            case LB_RULE_ERROR:
                return lbRule((Map<String, String>) object);
            case ILLEGAL_REMIND_ERROR:
                return illegalRemind((Map<String, String>) object);
            case LB_EPISODE_NUM_ERROR:
                return lbEpisodeNumWarn((Map<String, String>) object);
            case CHANGE_TAG_REMIND:
                return lsChannelTagChanged((Map<String, String>) object);
            case LS_START_REMIND:
                return lsStartRemind((Map<String, String>) object);
            case LS_DEL_REQUEST_FAIL:
                return getLsDelRequestFail((Map<String, String>) object);
            case LS_DEL_SUC:
                return getLsDelSuc((Map<String, String>) object);
            case LS_DEL_FAIL_REMIND:
                return getLsDelFailRemind((Map<String, String>) object);
            case LS_AUTO_DEL_REMIND:
                return getLsAutoDelRemind((Map<String, String>) object);
            case LB_ALBUM_UPDATE_REMIND:
                return lbAlbumUpdateRemind((Map<String, String>) object);
            case CAROUSEL_ERROR_COUNT_REMIND:
                return carouselErrorCountRemind((Map<String, String>) object);
            case AUTOMATION_RULE_ILLEGAL:
                return automationRuleIllegal((Map<String, String>) object);
            case DAOJU_REMIND:
                return daojuRemind((Map<String, String>) object);
            case STREAM_START_REMIND:
                return streamStartRemind((Map<String, String>) object);
            case STREAM_STOP_REMIND:
                return streamStopRemind((Map<String, String>) object);
            case LS_DEL_STREAM_FAIL_REMIND:
                return getLsDelStreamFailRemind((Map<String, String>) object);
            case PROGRAM_STARTTIME_DELAY_REMIND:
                return getTemplateProgramStartTimeDelayContent((Map<String, String>) object);
            case PROGRAM_STARTTIME_OFFLINE_REMIND:
                return getTemplateProgramStartTimeOFFLINEContent((Map<String, String>) object);
            case PROGRAM_CREATE_LIVE_NOTIFY:
                return programCreateLiveNotify((Map<String, String>) object);
            default:
                logger.info("not match type:" + type);
                return null;
        }
    }


    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder(512);
        sb.append("<table style=\"BORDER-COLLAPSE: collapse\" height=\"198\" width:\"1000\" >");
        sb.append(" <colgroup><col width=\"14%\"/><col width=\"42%\"/><col width=\"42%\"/></colgroup>");
        sb.append("<tbody>");
        sb.append("</tbody>");
        sb.append(checkList(null));
        sb.append(TABLE_END);
        System.out.print(sb.toString());
    }

    //http://xinqi.nei.pps.tv/categories.json
    public static String checkList(Map<String, Object> mapData) {
        List<String[]> list = new ArrayList<String[]>();
        ProgramCheckListData qipuData = (ProgramCheckListData) mapData.get("qipuDB");
        ProgramCheckListData localData = (ProgramCheckListData) mapData.get("localData");
        buildByData(list, localData, qipuData);
        Map<String, String> tdClassMap = new HashMap<String, String>();
        int titleStart = 0;
        int channelBaseStart = 3;
        int programBaseStart = channelBaseStart + 9;
        int onlineStart = programBaseStart + 13;
        int codeStart = onlineStart + 4;
        int streamStart = codeStart + 4;
        tdClassMap.put("*_0", "tdClassTitle");
        tdClassMap.put("*_" + channelBaseStart, "tdClassTitle");
        tdClassMap.put("*_" + programBaseStart, "tdClassTitle");
        tdClassMap.put("*_" + onlineStart, "tdClassTitle");
        tdClassMap.put("*_" + codeStart, "tdClassTitle");
        tdClassMap.put("*_" + streamStart, "tdClassTitle");
        StringBuffer sbBuffer = new StringBuffer(256);
        sbBuffer.append(HTML_PRE);
        sbBuffer.append(STYLE_PRE);
        sbBuffer.append(TD_CLASS);
        sbBuffer.append(STYLE_END);
        sbBuffer.append(BODY_PRE);
        if (qipuData.isHasError()) {
            sbBuffer.append(transRedSpan("处理奇谱数据时发生异常") + BR);
        }
        if (localData.isHasError()) {
            sbBuffer.append(transRedSpan("处理内部数据时发生异常") + BR);
        }
        sbBuffer.append("<table style=\"BORDER-COLLAPSE: collapse\" height=\"198\" width=\"1000\">");
        sbBuffer.append(" <colgroup><col width=\"16%\"/><col width=\"42%\"/><col width=\"42%\"/></colgroup>");
        sbBuffer.append("<tbody>");
        sbBuffer.append(makeTableRow(list, tdClassMap, TD_PRE_LEFT));
        sbBuffer.append("</tbody>");
        sbBuffer.append(TABLE_END);
        sbBuffer.append(BODY_END);
        sbBuffer.append(HTML_END);
        return sbBuffer.toString();
        //   return makeTableRow(list, tdClassMap);
    }

    private static void buildByData(List<String[]> list, ProgramCheckListData localData, ProgramCheckListData qipuData) {
        list.add(new String[]{"直播标题", "开始时间", "奇谱ID"});
        list.add(new String[]{localData.getProgramName(), localData.getStartTime(), addQipuLink(localData.getChannelQipuId()) + "|" + addQipuLink(localData.getProgramQipuId())});
        list.add(new String[]{"", "", ""});
        list.add(new String[]{"频道基本数据", "确认情况", "频道奇谱数据"});
        list.add(new String[]{"频道名称", localData.getChannelName(), qipuData.getChannelName()});
        list.add(new String[]{"弹幕开关", transPopUps(localData.isChannelAllowPopUps()), transPopUps(qipuData.isChannelAllowPopUps())});
        list.add(new String[]{"评论ID及显示", transQiTan(localData.getChannelQitanId(), localData.isChannelQiTanShow()), transQiTan(qipuData.getChannelQitanId(), qipuData.isChannelQiTanShow())});
        list.add(new String[]{"频道图片", transPic(localData.getDefaultLogoUrl()), transPic(qipuData.getDefaultLogoUrl())});

        list.add(new String[]{"启用状态", transEnable(localData.isEnable()), transEnable(qipuData.isEnable())});
        list.add(new String[]{"专题页地址", transSubjectUrl(localData.getSubjectUrl()), transSubjectUrl(qipuData.getSubjectUrl())});
        list.add(new String[]{"频道奇速地址", "", transQisu(qipuData.getCqisuUrl())});
        list.add(new String[]{"", "", ""});
        list.add(new String[]{"节目单", "确认情况", "节目奇谱数据"});
        list.add(new String[]{"标题", localData.getProgramName(), qipuData.getProgramName()});
        list.add(new String[]{"节目图片", transPic(localData.getProgramDefaultLogoUrl()), transPic(qipuData.getProgramDefaultLogoUrl())});
        list.add(new String[]{"伪静态播放地址", wrapLink(localData.getPageUrl()), ""});
        list.add(new String[]{"时间", localData.getStartTime() + "~" + localData.getEndTime(), qipuData.getStartTime() + "~" + qipuData.getEndTime()});
        list.add(new String[]{"弹幕开关", transPopUps(localData.isProgramAllowPopUps()), transPopUps(qipuData.isProgramAllowPopUps())});
        list.add(new String[]{"评论ID及显示", transQiTan(localData.getProgramQitanId(), localData.isProgramQiTanShow()), transQiTan(qipuData.getProgramQitanId(), qipuData.isProgramQiTanShow())});
        list.add(new String[]{"聊天室ID及开关及显示", transChatRoom(localData), transChatRoom(qipuData)});
        list.add(new String[]{"道具ID及显示", transDaoju(localData), transDaoju(qipuData)});
        list.add(new String[]{"付费状态", transBoss(localData.getProgramBoss()), transBoss(qipuData.getProgramBoss())});
        list.add(new String[]{"节目等级", localData.getEpisodeGrade(), ""});
        list.add(new String[]{"节目奇速地址", "", transQisu(qipuData.getPqisuUrl())});
        list.add(new String[]{"大小写分类", localData.getProgramTag(), qipuData.getProgramTag()});
        list.add(new String[]{"", "", ""});
        list.add(new String[]{"播控中心", "确认情况", "频道奇谱数据"});
        list.add(new String[]{"频道各端上线", transPlatformStatus(localData.getOnlinePlatforms(), localData.isPlatformEmpty()), transPlatformStatus(qipuData.getOnlinePlatforms(), qipuData.isPlatformEmpty())});
        list.add(new String[]{"节目各端上线", transPlatformStatus(localData.getProgramOnlinePlatforms(), localData.isProgramPlatformEmpty()), transPlatformStatus(qipuData.getProgramOnlinePlatforms(), qipuData.isProgramPlatformEmpty())});
        list.add(new String[]{"", "", ""});
        list.add(new String[]{"信号及编码状态", "", ""});
        list.add(new String[]{"端口状态", transServerStatus(localData), ""});
        list.add(new String[]{"编码状态", transStreamStatus(localData), ""});
        list.add(new String[]{"", "", ""});
        list.add(new String[]{"播放检测", "数据库流地址", "vrs流地址"});
        List<ChannelStreamData> channelStreamDataList = localData.getChannelStreamDataList();
        String[] checkFormatType = LinkData.CHECK_TYPE_ARR;
        //new String[]{"FLV", "HCDN", "TS"};
        boolean isDefaultStream = false;
        //  String[] defaultStream = new String[]{"默认码流", "", ""};
        Map<String, LinkData> qsMap = qipuData.getStreamMap();


        for (ChannelStreamData channelStreamData : channelStreamDataList) {
            String streamReplace = transToStreamType(channelStreamData.getStreamType());
            for (int j = 0; j < checkFormatType.length; j++) {
                if (LinkData.TYPE_DOLBY.equals(checkFormatType[j]) && !channelStreamData.isHasDolby()) {
                    continue;
                }
                list.add(new String[]{checkFormatType[j] + "(" + streamReplace + ")",
                        transStreamUrl(localData.getStreamMap(), channelStreamData.getStreamType() + "_" + checkFormatType[j]),
                        transStreamUrl(qsMap, channelStreamData.getStreamType() + "_" + checkFormatType[j])});
            }
        }
        list.add(new String[]{"VRS数据接口", transVRsVisitUrl(qipuData), transVrsVisitResult(qipuData)});
//        if (qipuData.isFree()) {
//
//        } else {
//            list.add(new String[]{"VRS数据接口", wrapLink(qipuData.getVrsVipUrl()), transVrsVisitResult(qipuData)});
//        }
    }

    private static String addQipuLink(long qipuId) {
        String link = ApiQipuUtil.getQipuEntityUrl(qipuId);
        return wrapLink("" + qipuId, link);
    }

    private static String transVRsVisitUrl(ProgramCheckListData data) {
        String retValue = "";
        if (data.isFree()) {
            if (data.getVrsFirstUrl() != null) {
                retValue += "第一次访问:" + wrapLink(data.getVrsFirstUrl()) + "<br>第二次访问:";
            }
            retValue += wrapLink(data.getVrsNormalUrl());
        } else {
            retValue = wrapLink(data.getVrsVipUrl());
        }
        return retValue;
    }

    private static String transVrsVisitResult(ProgramCheckListData data) {
        String retValue = "";
        if (data.getVrsFirstUrl() != null) {
            retValue += "第一次结果:" + transRedSpan(data.getVrsFirstResult()) + "<br>第二次结果:";
        }
        if (data.isVrsVisitRed()) {
            retValue += transRedSpan(data.getVrsVisitResult());
        } else {
            retValue += data.getVrsVisitResult();
        }
        return retValue;
    }

    private static String transSubjectUrl(String value) {
        if (value == null || value.trim().isEmpty()) {
            return "编辑未填写";
        }
        return wrapLink(value);
    }

    private static String transRedSpan(String value) {
        return SPAN_RED + value + SPAN_END;
    }

    private static String wrapLink(String showUrl) {
        return wrapLink(showUrl, null);
    }

    private static String wrapLink(String showUrl, String link) {
        if (link == null) {
            link = showUrl;
        }
        if (showUrl == null || showUrl.isEmpty()) {
            return "";
        }
        return "<a target=\"_blank\" href=\"" + link + "\">" + showUrl + "<a>";
    }

    public static String transToStreamType(String streamType) {
        if ("TOP_SPEED".equals(streamType)) {
            return "极速";
        } else if ("SMOOTH".equals(streamType)) {
            return "流畅";
        } else if ("HIGH_DEFINITION".equals(streamType)) {
            return "高清";
        } else if ("SUPER_CLEAR".equals(streamType)) {
            return "超清";
        } else if ("RESOLUTION_720P".equals(streamType)) {
            return "720P";
        } else if ("RESOLUTION_1080P".equals(streamType)) {
            return "1080P";
        } else if ("SPECIAL_H5".equals(streamType)) {
            return "H5";
        } else if ("RESOLUTION_2K".equals(streamType)) {
            return "2K";
        } else if ("RESOLUTION_4K".equals(streamType)) {
            return "4K";
        } else {
            return null;
        }
    }

    private static String transStreamUrl(Map<String, LinkData> map, String checkKey) {
        if (map == null) {
            return "";
        }
        LinkData linkData = map.get(checkKey);
        String retValue = transLink(linkData);
        if (linkData != null && linkData.getSpan() != null) {
            String spanDetail = String.format("[span=%s]", linkData.getSpan());
            retValue += BR + "&nbsp;&nbsp;" + (linkData.isSpanWarn() ? transRedSpan(spanDetail) : spanDetail);
        }
        return retValue;
    }

    public static boolean isAllClient(int clientSize) {
        return CLIENT_INUSE_NUMBER == clientSize;
    }

    private static String transPlatformStatus(List<String> list, boolean isEmpty) {
        if (isEmpty) {
            return transRedSpan("未设定播控");
        }
        if (list.size() == 0) {
            return transRedSpan("全端下线");
        } else if (isAllClient(list.size())) {
            return "全端上线";
        } else {
            return list.toString().replaceAll("\\[|\\]", "");
        }
    }

    private static String transStreamStatus(ProgramCheckListData data) {
        List<ChannelStreamData> list = data.getChannelStreamDataList();
        LinkData linkData = null;
        StringBuffer sb = new StringBuffer();
        if (list.isEmpty()) {
            sb.append(transRedSpan("没有流信息"));
        } else {
            for (ChannelStreamData temp : list) {
                linkData = temp.getPreView();
                sb.append(temp.getStreamName() + " &nbsp;");
                sb.append(transLink(linkData) + " &nbsp;");
                if (temp.isEncodeRed()) {
                    sb.append(transRedSpan("未编码"));
                } else {
                    sb.append("编码中");
                }
                sb.append(" &nbsp;");
                if (StringUtils.isEmpty(temp.getStreamAspectRatio())) {
                    sb.append(transRedSpan("画面比例未设定"));
                } else {
                    if (WARN_ASPECT_RATIO.equals(temp.getStreamAspectRatio())) {
                        sb.append(transRedSpan("画面比例 " + temp.getStreamAspectRatio()));
                    } else {
                        sb.append("画面比例 " + temp.getStreamAspectRatio());
                    }
                }
                sb.append(" &nbsp;");
                boolean isLater = false;
                String logDescr = null;
                if (temp.getLogType() == Constants.LogType.ENCODE_START.getIndex()) {
                    isLater = temp.getLogTime().compareTo(data.getStartTime()) > 0;
                    logDescr = " |最近编码时间&nbsp;" + temp.getLogTime();
                } else if (temp.getLogType() == Constants.LogType.ENCODE_STOP.getIndex()) {
                    isLater = temp.getLogTime().compareTo(data.getStartTime()) > 0;
                    logDescr = " |最近中断时间&nbsp;" + temp.getLogTime();
                }
                if (logDescr != null) {
                    sb.append(isLater ? transRedSpan(logDescr) : logDescr);
                }
                sb.append(BR);
            }
        }
        return sb.toString();
    }


    private static String transServerStatus(ProgramCheckListData data) {
        List<ChannelStreamData> list = data.getChannelStreamDataList();

        LinkData linkData = null;
        StringBuffer sb = new StringBuffer();
        if (list.isEmpty()) {
            sb.append(transRedSpan("没有流信息"));
        } else {
            for (ChannelStreamData temp : list) {
                linkData = temp.getServerStat();
                sb.append(temp.getStreamName() + " &nbsp;");
                sb.append(transLink(linkData) + " &nbsp;");
                if (temp.isVisitRed()) {
                    sb.append(transRedSpan("不能访问"));
                } else {
                    sb.append("可以访问");
                }
                if (linkData.isDolbyLink()) {
                    sb.append("|||" + linkData.getDolbyName() + " &nbsp;" + linkData.getDolbyUrl());
                }
                sb.append(BR);
            }
        }

        return sb.toString();
    }

    private static String transNull(String value) {
        return value == null ? "" : value;
    }

    private static String transLink(LinkData linkData) {
        if (linkData == null) {
            return "";
        }
        String retValue = null;
        if (linkData.isLinkFormat()) {
            retValue = wrapLink(linkData.getShowUrl(), linkData.getLink());
        } else {
            retValue = linkData.getShowUrl();
        }

        return retValue;
    }

    private static String transEnable(boolean flag) {
        return flag ? "启用" : transRedSpan("未启用");
    }

    private static String transEnableNoColor(boolean flag) {
        return flag ? "启用" : "未启用";
    }

    private static String transPic(String picUrl) {
        if (StringUtils.isEmpty(picUrl)) {
            return transRedSpan("没有图片");
        }
        return wrapLink(picUrl);
    }

    private static String transQiTan(long qitanId, boolean show) {
        return qitanId + "/" + (show ? "显示" : "隐藏");
    }

    private static String transDaoju(ProgramCheckListData data) {
        if (StringUtils.isEmpty(data.getDaojuId()) && data.isDaojuEnable()) {
            return transRedSpan("无/" + transEnableNoColor(data.isDaojuEnable()));
        } else {
            String retValue1 = StringUtils.isEmpty(data.getDaojuId()) ? "无" : data.getDaojuId();
            return retValue1 + "/" + transEnableNoColor(data.isDaojuEnable());
        }
    }

    private static String transChatRoom(ProgramCheckListData data) {
        return data.getChatRoomId() + "/" + transEnableNoColor(data.isChatEnable()) + "/" + (data.isChatDisplay() ? "显示" : "隐藏");
    }

    private static String transPopUps(boolean flag) {
        return flag ? "开" : "关";
    }

    private static String transBoss(String bossStats) {
        if (ProgramData.FREE.equals(bossStats)) {
            return "免费";
        } else if (ProgramData.PAIED_NOT_AVAILABLE.equals(bossStats)) {
            return "付费未划价";
        } else if (ProgramData.PAIED_AVAILABLE.equals(bossStats)) {
            return "付费已划价";
        }
        return bossStats;
    }

    private static String transQisu(String qisuUrl) {
        if (StringUtils.isEmpty(qisuUrl)) {
            return transRedSpan("未设定");
        } else {
            return wrapLink(qisuUrl);
        }
    }

    private static String makeTableRow(List<String[]> list, Map<String, String> tdClassMap, String tdPre) {
        StringBuilder sb = new StringBuilder(256);
        String[] content = null;
        StringBuilder classBuilder = new StringBuilder();
        for (int j = 0; j < list.size(); j++) {
            content = list.get(j);
            sb.append(TR_PRE);
            for (int i = 0; i < content.length; i++) {
                if (tdClassMap.containsKey(i + "_*")) {
                    classBuilder.append(tdClassMap.get(i + "_*") + " ");
                }
                if (tdClassMap.containsKey("*_" + j)) {
                    classBuilder.append(tdClassMap.get("*_" + j) + " ");
                }
                if (tdClassMap.containsKey(i + "_" + j)) {
                    classBuilder.append(tdClassMap.get(i + "_" + j) + " ");
                }
                if (classBuilder.length() > 0) {
                    sb.append("<td class=\"" + classBuilder.toString() + "\"  >");
                    classBuilder.delete(0, classBuilder.length());
                } else {
                    sb.append(tdPre);
                }
                sb.append(content[i]);
                sb.append(TD_END);
            }
            sb.append(TR_END);
        }
        return sb.toString();
    }

    private static String makeTableRow(List<String[]> list, String[] tdStyleArr) {
        StringBuilder sb = new StringBuilder(256);
        String[] content = null;
        for (int j = 0; j < list.size(); j++) {
            content = list.get(j);
            sb.append(TR_PRE);
            for (int i = 0; i < content.length; i++) {
                if (tdStyleArr.length > i && tdStyleArr[i] != null) {
                    sb.append("<td class=\"" + tdStyleArr[i] + "\"  >");
                } else {
                    sb.append(TD_PRE);
                }
                sb.append(content[i]);
                sb.append(TD_END);
            }
            sb.append(TR_END);
        }
        return sb.toString();
    }

    private static String lbAddProgram(Map<String, String> map) {
        return replaceUtil.format(TEMPLATE_LB_ADD_PROGRAM_ERROR, map);
    }

    private static String lbRule(Map<String, String> map) {
        return replaceUtil.format(TEMPLATE_LB_RULE_ERROR, map);
    }

    private static String illegalRemind(Map<String, String> map) {
        return replaceUtil.format(TEMPLATE_ILLEGAL_REMIND_ERROR, map);
    }

    private static String lbEpisodeNumWarn(Map<String, String> map) {
        return replaceUtil.format(TEMPLATE_LB_EPISODE_NUM_ERROR, map);
    }

    private static String lsChannelTagChanged(Map<String, String> map) {
        return replaceUtil.format(TEMPLATE_CHANGE_TAG_REMIND, map);
    }

    private static String lsStartRemind(Map<String, String> map) {
        return replaceUtil.format(TEMPLATE_LS_START_REMIND, map);
    }

    private static String getLsDelRequestFail(Map<String, String> map) {
        return replaceUtil.format(TEMPLATE_LS_DEL_REQUEST_FAIL, map);
    }

    private static String getLsDelSuc(Map<String, String> map) {
        return replaceUtil.format(TEMPLATE_LS_DEL_SUC, map);
    }

    private static String getLsDelFailRemind(Map<String, String> map) {
        return replaceUtil.format(TEMPLATE_LS_DEL_FAIL_REMIND, map);
    }

    private static String getLsAutoDelRemind(Map<String, String> map) {
        return replaceUtil.format(TEMPLATE_LS_AUTO_DEL_REMIND, map);
    }

    private static String lbAlbumUpdateRemind(Map<String, String> map) {
        return replaceUtil.format(TEMPLATE_LB_ALBUM_UPDATE_REMIND, map);
    }

    private static String carouselErrorCountRemind(Map<String, String> map) {
        return replaceUtil.format(TEMPLATE_CAROUSEL_ERROR_COUNT_REMIND, map);
    }

    private static String automationRuleIllegal(Map<String, String> map) {
        return replaceUtil.format(TEMPLATE_AUTOMATION_RULE_ILLEGAL_TITLE, map);
    }

    private static String daojuRemind(Map<String, String> map) {
        return replaceUtil.format(TEMPLATE_DAOJU_REMIND, map);
    }

    private static String streamStartRemind(Map<String, String> map) {
        return replaceUtil.format(TEMPLATE_STREAM_START_REMIND, map);
    }

    private static String streamStopRemind(Map<String, String> map) {
        return replaceUtil.format(TEMPLATE_STREAM_STOP_REMIND, map);
    }

    private static String getLsDelStreamFailRemind(Map<String, String> map) {
        return replaceUtil.format(TEMPLATE_LS_DEL_STREAM_FAIL_REMIND, map);
    }

    private static String getTemplateProgramStartTimeDelayContent(Map<String, String> map) {
        StringBuffer content = new StringBuffer();
        content.append(transRedSpan(TEMPLATE_PROGRAM_STARTTIME_DELAY_TITLE_RED))
                .append(TEMPLATE_PROGRAM_STARTTIME_DELAY_CONTENT)
                .append(transRedSpan(TEMPLATE_PROGRAM_EDITSTARTTIME_DELAY_RED))
                .append(TEMPLATE_PROGRAM_STOPTIME)
                .append(transRedSpan(TEMPLATE_PROGRAM_REMIND_RED));
        return replaceUtil.format(content.toString(), map);
    }

    private static String getTemplateProgramStartTimeOFFLINEContent(Map<String, String> map) {
        StringBuffer content = new StringBuffer();
        content.append(transRedSpan(TEMPLATE_PROGRAM_OFFLINE_TITLE_RED))
                .append(TEMPLATE_PROGRAM_STARTTIME_DELAY_CONTENT)
                .append(transRedSpan(TEMPLATE_PROGRAM_EDITSTARTTIME_DELAY_RED))
                .append(TEMPLATE_PROGRAM_STOPTIME)
                .append(transRedSpan(TEMPLATE_PROGRAM_REMIND_RED));
        return replaceUtil.format(content.toString(), map);
    }

    private static String programCreateLiveNotify(Map<String, String> map) {
        return replaceUtil.format(TEMPLATE_PROGRAM_CREATE_LIVE_NOTIFY, map);
    }
}
